import * as Redux from 'redux';
import { connect } from 'react-redux';

import * as Nav from './ui/nav';
import * as Common from './model/common';

function getMapDispatchToProps(selfMapDispatch: (dispatch: Redux.Dispatch<any>, connected: any) => any, mapKeys: any) {
    return (dispatch: Redux.Dispatch<any>) => {
        let connected = {};
        const connectList: any[] = [
            Nav.connect,
            Common.connect,
        ];
        connectList.forEach(fun => {
            connected = fun(dispatch, connected, mapKeys);
        });
        return selfMapDispatch ? selfMapDispatch(dispatch, connected) : connected;
    };
}

export default (mapStoreToProps: any, mapDispatchToProps: any, componentClass: any, mapKeys: any = {}): any => connect(mapStoreToProps,
    getMapDispatchToProps(mapDispatchToProps, mapKeys),
    (a, b, c) => ({ ...a, ...b, ...c }),
    {})(componentClass);